var express = require("express");
var http = require('http');
var users = require("./routes/users"),
    events = require("./routes/events"),
	teams = require("./routes/teams"),
	messages = require("./routes/messages"),
	pictures = require("./routes/pictures"),
    comments = require("./routes/comments"),
    indexes = require("./routes/indexes");
	
// function logErrors(err, req, res, next) {
//   console.error(err.stack);
//   next(err);
// }

// function clientErrorHandler(err, req, res, next) {
//   if (req.xhr) {
//     res.send(500, { error: 'Something blew up!' });
//   } else {
//     next(err);
//   }
// }

// function errorHandler(err, req, res, next) {
//   res.status(500);
//   res.render('error', { error: err });
// }

var app = express();

app.configure(function(){
	app.use(express.bodyParser());
	app.use(express.methodOverride());
    app.use(express.cookieParser());
    app.use(express.session({secret: '1234567890QWERTY'}));
    app.use(app.router);
	// app.use(logErrors);
	// app.use(clientErrorHandler);
	// app.use(errorHandler);

});

app.get('/', function(req, res) { 
    res.json({ 'MONGOLAB_DB' : process.env.MONGOLAB_DB, 
        'VCAP_APP_PORT' : process.env.VCAP_APP_PORT});});


//users
app.post('/new', users.createUser);   //okay
app.post('/login', users.loginUsers); //okay

app.get('/users/:userid', users.getUser);   //okay
app.put('/users/:userid', users.updateUser);    //okay
app.del('/users/:userid', users.deleteUser);    //okay

app.get('/users/:userid/skills', users.getUserSkills); //okay
app.put('/users/:userid/skills', users.updateUserSkills); //okay

app.put('/users/:userid/friends', users.addFriend); //okay
app.get('/users/:userid/friends', users.getFriends); //okay

app.del('/users/:userid/friends', users.deleteFriend); //okay

app.get('/users/:userid/events', users.readUserEvents); //okay

app.get('/users/:userid/teams', users.readUserTeams); //okay

app.get('/users/:userid/inbox', users.readUserInbox); //okay

app.get('/users/:userid/outbox', users.readUserOutbox); //okay

//events
app.post('/events', events.createEvent);    //ok
app.get('/events', events.getEvents);       //ok

app.get('/events/:eventid', events.getEvent);       //ok
app.put('/events/:eventid', events.updateEvent);    //ok
app.del('/events/:eventid', events.deleteEvent);    //ok

app.post('/events/:eventid/members', events.addEventMembers);    //ok
app.get('/events/:eventid/members', events.getEventMembers);    //ok

app.del('/events/:eventid/members/:membername', events.removeEventMember);  //ok

app.post('/events/:eventid/results', events.addEventResults);   //ok
app.get('/events/:eventid/results', events.getEventResults);        //ok

app.get('/events_near', events.getEventsNearLocation);  //ok

//teams

//teams
app.post('/teams', teams.createTeam); // create new team in DB.
app.get('/teams/:teamid', teams.getTeam);  //get exists team, send the team ID in rest request. 
app.put('/teams/:teamid', teams.updateTeam); //update exists team, send the team ID in rest request. 
app.del('/teams/:teamid', teams.deleteTeam); //delete exists team, send the team ID in rest request. 
app.post('/teams/:teamid/members', teams.addTeamMember); //add existing user to exists team to her list members, send the team ID in rest request and the nick name of member in the boday.
app.get('/teams/:teamid/members', teams.readTeamMembers); //get the list of members in the team. send the team ID  in rest request.
app.del('/teams/:teamid/members/:memberNickName', teams.deleteTeamMember);//send the team ID and the member Nick Name in rest request and delete the member from team's list members 

app.get('/teams_near', teams.getTeamsNearLocation);  //ok

//messages
app.post('/messages', messages.createMessage);
app.del('/messages', messages.deleteMessage);

//pictures
app.post('/pictures', pictures.uploadPicture);
app.get('/pictures/:picid',pictures.getPicture);

//un comment when ready
// app.get('/pictures/:messageid', pictures.readPicture);
// app.del('/pictures/:messageid', pictures.deletePicture);


//comments
app.post('/comments/', comments.createComment); //create new comment in DB.//ok
app.del('/comments/:commentid', comments.deleteComment);//delete comment by id//ok
app.get('/comments/:itemid', comments.readComments);//get comments by itemid//ok

//indices
app.get('/createIndexes',indexes.setIndices);

//run app
http.createServer(app).listen(process.env.PORT || 3000, function(){
  console.log('Express server listening on port ' + process.env.PORT);
});
//app.listen(port,host);